/*
* menu.c
*
* Created: 16.04.2014 08:36:53
*  Author: markus.hufschmid
*/
#include "defines.h"
#include <asf.h>
#include "globals.h"
#include <stdio.h>
#include "OLED.h"
#include "menu.h"
#include "demo.h"



void menu(void){
	char printString[12];
	
	while (state == MENU){
		switch (menu_state)
		{
			case M_PREDIVE:
				Fill_RAM(0x00, 0x00);
				print_string_zoomed("PREDIVE",22,7,LT_GREY, BLACK, 2);
				draw_horizontal_line(0,31,127,LT_GREY);
				sprintf(printString, "%2d:%02d",rtc_hour, rtc_min);
				print_string_zoomed(printString, 34, 36, SKY_BLUE, BLACK,2);
				sprintf(printString, "%2d.%2d.%4d",rtc_day, rtc_month, rtc_year);
				print_string_zoomed(printString, 4, 56, SKY_BLUE, BLACK,2);
				draw_horizontal_line(0,75,127,LT_GREY);
				print_string_zoomed("O2",2,77,LT_GREY,BLACK,1);
				sprintf(printString,"%2d",percentage_O2);
				print_string_zoomed(printString,2,85,SKY_BLUE,BLACK,2);
				print_string_zoomed("%",26,93,SKY_BLUE,BLACK,1);
				print_string_zoomed("PO2",66,77,LT_GREY,BLACK,1);
				sprintf(printString,"%3.1f",max_partial_O2);
				print_string_zoomed(printString,66,85,SKY_BLUE,BLACK,2);
				print_string_zoomed("bar",102,93,SKY_BLUE,BLACK,1);
				draw_horizontal_line(0,101,127,LT_GREY);
				print_string_zoomed("MOD",2,103,LT_GREY,BLACK,1);
				sprintf(printString,"%4.1f",max_operating_depth);
				print_string_zoomed(printString,2,111,SKY_BLUE,BLACK,2);
				print_string_zoomed("m",50,119,SKY_BLUE,BLACK,1);
				print_string_zoomed("H2O",66,103,LT_GREY,BLACK,1);
				if (salt_water){
					print_string_zoomed("SALT",66,111,SKY_BLUE, BLACK,2);
					} else {
					print_string_zoomed("FRESH",66,111,SKY_BLUE, BLACK,2);
				}
				draw_horizontal_line(0,127,127,LT_GREY);
				draw_vertical_line(0,75,127,LT_GREY);
				draw_vertical_line(63,75,127,LT_GREY);
				draw_vertical_line(127,75,127,LT_GREY);

			
				menu_state = predive_menu();
				break;
			case M_SETWATCH:
				Fill_RAM(0x00, 0x00);
				print_string_zoomed("SETWATCH",16,7,LT_GREY, BLACK, 2);
				draw_horizontal_line(0,31,127,LT_GREY);
				sprintf(printString,"Time %2d:%02d",rtc_hour, rtc_min);
				print_string_zoomed(printString, 4, 40, SKY_BLUE, BLACK,2);
				sprintf(printString,"Day     %2d",rtc_day);
				print_string_zoomed(printString, 4, 64, SKY_BLUE, BLACK,2);
				sprintf(printString,"Month   %2d",rtc_month);
				print_string_zoomed(printString, 4, 88, SKY_BLUE, BLACK,2);
				sprintf(printString,"Year  %4d",rtc_year);
				print_string_zoomed(printString, 4, 112, SKY_BLUE, BLACK,2);
				menu_state = setwatch_menu();
				break;
			case M_SETDIVE:
				Fill_RAM(0x00, 0x00);
				print_string_zoomed("SETDIVE",16,7,LT_GREY, BLACK, 2);
				draw_horizontal_line(0,31,127,LT_GREY);
				sprintf(printString,"Gas    %2d%%",percentage_O2);
				print_string_zoomed(printString, 4, 40, SKY_BLUE, BLACK,2);
				sprintf(printString,"PO2 %3.1fbar",max_partial_O2);
				print_string_zoomed(printString, 4, 64, SKY_BLUE, BLACK,2);
				print_string_zoomed("H2O",4,88, SKY_BLUE, BLACK,2);
				if (salt_water){
					print_string_zoomed(" SALT",64,88,SKY_BLUE, BLACK,2);
					} else {
					print_string_zoomed("FRESH",64,88,SKY_BLUE, BLACK,2);
				}
				compute_MOD();
				sprintf(printString,"MOD  %4.1fm",max_operating_depth);
				print_string_zoomed(printString,4,112,SKY_BLUE, BLACK,2);
				menu_state = setdive_menu();
				break;
			case M_DEMO:
				Fill_RAM(0x00,0x00);
				print_string_zoomed("DEMO",40,7,LT_GREY, BLACK, 2);
				draw_horizontal_line(0,31,127,LT_GREY);
				print_string_zoomed("Press DOWN to start",7,75,SKY_BLUE, BLACK,1);
				menu_state = demo_menu();
				break;
			case M_AFTERDIVE:
				Fill_RAM(0x00, 0x00);
				print_string_zoomed("AFTERDIVE",10,7,LT_GREY, BLACK, 2);
				draw_horizontal_line(0,31,127,LT_GREY);
				draw_horizontal_line(0,49,127,LT_GREY);
				print_string_zoomed("Surface",2,51,LT_GREY,BLACK,1);
				sprintf(printString,"%2lu:%02lu",surfaced_time/3600,(surfaced_time/60)%60);
				print_string_zoomed(printString,67,59,SKY_BLUE,BLACK,2);
				draw_horizontal_line(0,75,127,LT_GREY);
				print_string_zoomed("Desat ",2,77,LT_GREY,BLACK,1);
				sprintf(printString,"%2d:%02d",desat_time_minutes/60,desat_time_minutes%60);
				print_string_zoomed(printString,67,85,SKY_BLUE,BLACK,2);
				draw_horizontal_line(0,101,127,LT_GREY);				
				print_string_zoomed("No Flight",2,103,LT_GREY,BLACK,1);
				sprintf(printString,"%2d:%02d",no_flight_time_minutes/60,no_flight_time_minutes%60);
				print_string_zoomed(printString,67,111,SKY_BLUE,BLACK,2);
				draw_horizontal_line(0,127,127,LT_GREY);
				draw_vertical_line(0,49,127,LT_GREY);
				draw_vertical_line(127,49,127,LT_GREY);
				menu_state = afterdive_menu();
				break;
			case M_LOGBOOK:
				Fill_RAM(0x00, 0x00);
				print_string_zoomed("LOGBOOK",22,7,LT_GREY, BLACK, 2);
				draw_horizontal_line(0,31,127,LT_GREY);
/*				draw_horizontal_line(0,49,127,LT_GREY);
				print_string_zoomed("Surface",2,51,LT_GREY,BLACK,1);
				sprintf(printString,"%2lu:%02lu",surfaced_time/3600,(surfaced_time/60)%60);
				print_string_zoomed(printString,67,59,SKY_BLUE,BLACK,2);
				draw_horizontal_line(0,75,127,LT_GREY);
				print_string_zoomed("Desat ",2,77,LT_GREY,BLACK,1);
				sprintf(printString,"%2d:%02d",desat_time_minutes/60,desat_time_minutes%60);
				print_string_zoomed(printString,67,85,SKY_BLUE,BLACK,2);
				draw_horizontal_line(0,101,127,LT_GREY);
				print_string_zoomed("No Flight",2,103,LT_GREY,BLACK,1);
				sprintf(printString,"%2d:%02d",no_flight_time_minutes/60,no_flight_time_minutes%60);
				print_string_zoomed(printString,67,111,SKY_BLUE,BLACK,2);
				draw_horizontal_line(0,127,127,LT_GREY);
				draw_vertical_line(0,49,127,LT_GREY);
				draw_vertical_line(127,49,127,LT_GREY);
*/
				menu_state = afterdive_menu();
				break;
			
		}
	}
}


uint16_t predive_menu(void){
	uint8_t menu_item = 0;
	char printString[12];
	
	while (state == MENU){
		if (new_data){
			new_data = 0;
			sprintf(printString, "%2d:%02d",rtc_hour, rtc_min);
			print_string_zoomed(printString, 34, 36, SKY_BLUE, BLACK,2);
			sprintf(printString, "%2d.%2d.%4d",rtc_day, rtc_month, rtc_year);
			print_string_zoomed(printString, 4, 56, SKY_BLUE, BLACK,2);
		}
		switch (menu_item){
			case 0:
			if (key_right_flag){
				key_right_flag = 0;
				return M_SETDIVE;
			}
			if (key_left_flag){
				key_left_flag = 0;
				return M_AFTERDIVE;
			}
			break;
		}
	}
	return M_PREDIVE;
}

uint16_t afterdive_menu(void){
	uint8_t menu_item = 0;
	char printString[12];
	key_down_flag = 0;
	
	while (state == MENU){
		if (new_data){
			new_data = 0;
			sprintf(printString,"%2lu:%02lu",surfaced_time/3600,(surfaced_time/60)%60);
			print_string_zoomed(printString,67,59,SKY_BLUE,BLACK,2);
			sprintf(printString,"%2d:%02d",desat_time_minutes/60,desat_time_minutes%60);
			print_string_zoomed(printString,67,85,SKY_BLUE,BLACK,2);
			sprintf(printString,"%2d:%02d",no_flight_time_minutes/60,no_flight_time_minutes%60);
			print_string_zoomed(printString,67,111,SKY_BLUE,BLACK,2);
		}
		switch (menu_item){
			case 0:
			if (key_right_flag){
				key_right_flag = 0;
				return M_PREDIVE;
			}
			if (key_left_flag){
				key_left_flag = 0;
				return M_DEMO;
			}
			break;
		}
	}
	return M_PREDIVE;
}

//uint16_t logbook_menu(void){
	//uint8_t menu_item = 0;
	//char printString[12];
	//key_down_flag = 0;
	//uint16_t log_nr, max_log_nr;
	//
	//max_log_nr = get_nr_of_dives();
	//while (state == MENU){
		//switch (menu_item){
			//case 0:
				//if (key_right_flag){
					//key_right_flag = 0;
					//return M_PREDIVE;
				//}
				//if (key_left_flag){
					//key_left_flag = 0;
					//return M_DEMO;
				//}
				//if (key_down_flag){
					//key_down_flag = 0;
					//menu_item++;
	///*				sprintf(printString, "%2d", rtc_hour);
					//print_string_zoomed(printString,64,40, BLACK, SKY_BLUE,2);
	//*/
				//}
				//break;
			//case 1:
				//if (key_right_flag){
					//key_right_flag = 0;
					//log_nr++;
					//if (log_nr > max_log_nr){
						//log_nr = 1;
					//}
///*					sprintf(printString, "%2d", rtc_hour);
					//print_string_zoomed(printString,64,40, BLACK, SKY_BLUE, 2);
//*/
				//}
				//if (key_left_flag){
					//key_left_flag = 0;
					//log_nr--;
					//if (log_nr < 1){
						//log_nr = max_log_nr;
					//}
///*					
					//sprintf(printString, "%2d", rtc_hour);
					//print_string_zoomed(printString,64,40, BLACK, SKY_BLUE, 2);
//*/
				//}
				//if (key_down_flag){
					//key_down_flag = 0;
					//menu_item = 0;
///*					
					//sprintf(printString, "%2d", rtc_hour);
					//print_string_zoomed(printString,64,40,SKY_BLUE, BLACK,2);
					//sprintf(printString, "%02d", rtc_min);
					//print_string_zoomed(printString,100,40,BLACK,SKY_BLUE, 2);
//*/
				//}
				//break;
		//}
	//}
	//return M_PREDIVE;
//}

uint16_t demo_menu(void){
	key_down_flag = 0;
	while (state == MENU){
		if (key_right_flag){
			key_right_flag = 0;
			return M_AFTERDIVE;
		}
		if (key_left_flag){
			key_left_flag = 0;
			return M_SETWATCH;
		}
		if (key_down_flag){
			key_down_flag = 0;
			demo();
			return M_DEMO;
		}
	}
	return M_PREDIVE;
}

uint16_t setwatch_menu(void){
	uint8_t menu_item = 0;
	char printString[12];
	
	key_down_flag = 0;
	while (state == MENU){
		switch (menu_item){
			case 0:
			if (key_right_flag){
				key_right_flag = 0;
				return M_DEMO;
			}
			if (key_left_flag){
				key_left_flag = 0;
				return M_SETDIVE;
			}
			if (key_down_flag){
				key_down_flag = 0;
				menu_item++;
				sprintf(printString, "%2d", rtc_hour);
				print_string_zoomed(printString,64,40, BLACK, SKY_BLUE,2);
			}
			break;
			case 1:
			if (key_right_flag){
				key_right_flag = 0;
				rtc_hour++;
				if (rtc_hour > 23){
					rtc_hour = 0;
				}
				sprintf(printString, "%2d", rtc_hour);
				print_string_zoomed(printString,64,40, BLACK, SKY_BLUE, 2);
			}
			if (key_left_flag){
				key_left_flag = 0;
				rtc_hour--;
				if (rtc_hour < 0){
					rtc_hour = 23;
				}
				sprintf(printString, "%2d", rtc_hour);
				print_string_zoomed(printString,64,40, BLACK, SKY_BLUE, 2);
			}
			if (key_down_flag){
				key_down_flag = 0;
				menu_item++;
				sprintf(printString, "%2d", rtc_hour);
				print_string_zoomed(printString,64,40,SKY_BLUE, BLACK,2);
				sprintf(printString, "%02d", rtc_min);
				print_string_zoomed(printString,100,40,BLACK,SKY_BLUE, 2);
			}
			break;
			case 2:
			if (key_right_flag){
				key_right_flag = 0;
				rtc_min++;
				if (rtc_min > 59){
					rtc_min = 0;
				}
				sprintf(printString, "%02d", rtc_min);
				print_string_zoomed(printString,100,40,BLACK, SKY_BLUE, 2);
			}
			if (key_left_flag){
				key_left_flag = 0;
				rtc_min--;
				if (rtc_min < 0){
					rtc_min = 59;
				}
			}
			sprintf(printString, "%02d", rtc_min);
			print_string_zoomed(printString,100,40,BLACK,SKY_BLUE, 2);
			if (key_down_flag){
				key_down_flag = 0;
				menu_item++;
				sprintf(printString, "%02d", rtc_min);
				print_string_zoomed(printString,100,40,SKY_BLUE, BLACK,2);
				sprintf(printString, "%2d", rtc_day);
				print_string_zoomed(printString,100,64,BLACK, SKY_BLUE,2);
			}
			break;
			case 3:
			if (key_right_flag){
				key_right_flag = 0;
				rtc_day++;
				if (rtc_day > 31){
					rtc_day = 1;
				}
				sprintf(printString, "%2d", rtc_day);
				print_string_zoomed(printString,100,64,BLACK, SKY_BLUE, 2);
			}
			if (key_left_flag){
				key_left_flag = 0;
				rtc_day--;
				if (rtc_day < 0){
					rtc_day = 31;
				}
				sprintf(printString, "%2d", rtc_day);
				print_string_zoomed(printString,100,64,BLACK, SKY_BLUE, 2);
			}
			if (key_down_flag){
				key_down_flag = 0;
				menu_item++;
				sprintf(printString, "%2d", rtc_day);
				print_string_zoomed(printString,100,64,SKY_BLUE, BLACK,2);
				sprintf(printString, "%2d", rtc_month);
				print_string_zoomed(printString,100,88,BLACK, SKY_BLUE,2);
			}
			break;
			case 4:
			if (key_right_flag){
				key_right_flag = 0;
				rtc_month++;
				if (rtc_month > 12){
					rtc_month = 1;
				}
				sprintf(printString, "%2d", rtc_month);
				print_string_zoomed(printString,100,88,BLACK, SKY_BLUE, 2);
			}
			if (key_left_flag){
				key_left_flag = 0;
				rtc_month--;
				if (rtc_month < 0){
					rtc_month = 1;
				}
				sprintf(printString, "%2d", rtc_month);
				print_string_zoomed(printString,100,88,BLACK, SKY_BLUE, 2);
			}
			if (key_down_flag){
				key_down_flag = 0;
				menu_item++;
				sprintf(printString, "%2d", rtc_month);
				print_string_zoomed(printString,100,88,SKY_BLUE, BLACK,2);
				sprintf(printString, "%4d", rtc_year);
				print_string_zoomed(printString,76,112,BLACK, SKY_BLUE,2);
			}
			break;
			case 5:
			if (key_right_flag){
				key_right_flag = 0;
				rtc_year++;
				if (rtc_year > 2100){
					rtc_year = 2100;
				}
				sprintf(printString, "%4d", rtc_year);
				print_string_zoomed(printString,76,112,BLACK, SKY_BLUE, 2);
			}
			if (key_left_flag){
				key_left_flag = 0;
				rtc_year--;
				if (rtc_year < 2014){
					rtc_year = 2014;
				}
				sprintf(printString, "%4d", rtc_year);
				print_string_zoomed(printString,76,112,BLACK, SKY_BLUE, 2);
			}
			if (key_down_flag){
				key_down_flag = 0;
				menu_item = 0;
				sprintf(printString, "%4d", rtc_year);
				print_string_zoomed(printString,76,112,SKY_BLUE, BLACK,2);
			}
			break;
		}
	}
	return M_PREDIVE;
}

uint16_t setdive_menu(void){
	uint8_t menu_item = 0;
	char printString[12];
	
	key_down_flag = 0;
	while (state == MENU){
		switch (menu_item){
			case 0:
			if (key_right_flag){
				key_right_flag = 0;
				return M_SETWATCH;
			}
			if (key_left_flag){
				key_left_flag = 0;
				return M_PREDIVE;
			}
			if (key_down_flag){
				key_down_flag = 0;
				menu_item++;
				sprintf(printString, "%2d", percentage_O2);
				print_string_zoomed(printString,88,40, BLACK, SKY_BLUE,2);
			}
			break;
			case 1:
			if (key_right_flag){
				key_right_flag = 0;
				percentage_O2++;
				if (percentage_O2 > 40){
					percentage_O2 = 40;
				}
				sprintf(printString, "%2d", percentage_O2);
				print_string_zoomed(printString,88,40, BLACK, SKY_BLUE, 2);
				compute_MOD();
				sprintf(printString,"%4.1f",max_operating_depth);
				print_string_zoomed(printString,64,112,SKY_BLUE, BLACK,2);
			}
			if (key_left_flag){
				key_left_flag = 0;
				percentage_O2--;
				if (percentage_O2 < 21){
					percentage_O2 = 21;
				}
				sprintf(printString, "%2d", percentage_O2);
				print_string_zoomed(printString,88,40, BLACK, SKY_BLUE, 2);
				compute_MOD();
				sprintf(printString,"%4.1f",max_operating_depth);
				print_string_zoomed(printString,64,112,SKY_BLUE, BLACK,2);
			}
			if (key_down_flag){
				key_down_flag = 0;
				menu_item++;
				sprintf(printString, "%2d", percentage_O2);
				print_string_zoomed(printString,88,40,SKY_BLUE, BLACK,2);
				sprintf(printString, "%3.1f", max_partial_O2);
				print_string_zoomed(printString,52,64,BLACK,SKY_BLUE, 2);
			}
			break;
			case 2:
			if (key_right_flag){
				key_right_flag = 0;
				max_partial_O2 += 0.1;
				if (max_partial_O2 > 1.6){
					max_partial_O2 = 1.6;
				}
				sprintf(printString, "%3.1f", max_partial_O2);
				print_string_zoomed(printString,52,64,BLACK, SKY_BLUE, 2);
				compute_MOD();
				sprintf(printString,"%4.1f",max_operating_depth);
				print_string_zoomed(printString,64,112,SKY_BLUE, BLACK,2);
			}
			if (key_left_flag){
				key_left_flag = 0;
				max_partial_O2 -= 0.1;
				if (max_partial_O2 < 1.2){
					max_partial_O2 = 1.2;
				}
			}
			sprintf(printString, "%3.1f", max_partial_O2);
			print_string_zoomed(printString,52,64,BLACK,SKY_BLUE, 2);
			compute_MOD();
			sprintf(printString,"%4.1f",max_operating_depth);
			print_string_zoomed(printString,64,112,SKY_BLUE, BLACK,2);
			if (key_down_flag){
				key_down_flag = 0;
				menu_item++;
				sprintf(printString, "%3.1f", max_partial_O2);
				print_string_zoomed(printString,52,64,SKY_BLUE, BLACK,2);
				if (salt_water){
					print_string_zoomed(" SALT",64,88,BLACK, SKY_BLUE,2);
					} else {
					print_string_zoomed("FRESH",64,88,BLACK, SKY_BLUE,2);
				}
			}
			break;
			case 3:
			if (key_right_flag){
				key_right_flag = 0;
				salt_water = 1 - salt_water;
				if (salt_water){
					print_string_zoomed(" SALT",64,88,BLACK, SKY_BLUE,2);
					} else {
					print_string_zoomed("FRESH",64,88,BLACK, SKY_BLUE,2);
				}
			}
			if (key_left_flag){
				key_left_flag = 0;
				salt_water = 1 - salt_water;
				if (salt_water){
					print_string_zoomed(" SALT",64,88,BLACK, SKY_BLUE,2);
					} else {
					print_string_zoomed("FRESH",64,88,BLACK, SKY_BLUE,2);
				}
			}
			if (key_down_flag){
				key_down_flag = 0;
				menu_item = 0;
				if (salt_water){
					print_string_zoomed(" SALT",64,88,SKY_BLUE, BLACK,2);
					} else {
					print_string_zoomed("FRESH",64,88,SKY_BLUE, BLACK,2);
				}
			}
			break;
		}
	}
	return M_PREDIVE;
}

void compute_MOD(void){
	float percentage_O2_flt;
	
	percentage_O2_flt = (float)percentage_O2/100.0;
	percentage_N2 = 1.0 - percentage_O2_flt;
	max_operating_depth = (max_partial_O2/percentage_O2_flt - 1.0)*10.0;
}